<template>
  <!-- 生产线边库存弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="300px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="140px">
      <el-form-item label="数量" prop="materialQuantity" label-width="50px">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.materialQuantity"
          placeholder="请输入数量"
          clearable
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer" style="margin-top: -20px">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >修 改</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";

defineOptions({
  name: "MESPRODUCESTORAGEEDIT",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    console.log("newVal");
    console.log(newVal);
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  materialQuantity: [
    {
      required: true,
      message: "请输入数字",
      trigger: "blur",
    },
    {
      pattern: /^\d+.?\d+$/,
      message: "请输入数字",
      trigger: "blur",
    },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  plantId: "",
  materialCode: "",
  workCenterCode: "",
  materialQuantity: "",
  quantityUnit: "",
  isValid: "",
  isFeedMaterial: "",
  stockFrom: "",
  enabled: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        plantId,
        materialCode,
        workCenterCode,
        materialQuantity,
        quantityUnit,
        isValid,
        isFeedMaterial,
        stockFrom,
        enabled,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        plantId,
        materialCode,
        workCenterCode,
        materialQuantity,
        quantityUnit,
        isValid,
        isFeedMaterial,
        stockFrom,
        enabled,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.plantId = "";
  formData.materialCode = "";
  formData.workCenterCode = "";
  formData.materialQuantity = "";
  formData.quantityUnit = "";
  formData.isValid = "";
  formData.isFeedMaterial = "";
  formData.stockFrom = "";
  formData.enabled = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 周转卡表保存提交 */
function handleSubmit() {
  console.log("formRef", formRef);

  formRef.value.validate(async (valid: any) => {
    if (valid) {
      console.log("valid", valid);
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mesproducingstockrecord/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

const status = [
  { code: "1", name: "初始打印" },
  { code: "2", name: "报工关联" },
];

const cardstatus = [
  { code: "1", name: "合格", value: "1" },
  { code: "2", name: "不合格", value: "2" },
  { code: "3", name: "返修", value: "3" },
];

let processedValue = computed(() => {
  return chooseTurnoverType();
});
function chooseTurnoverType() {
  for (let card of cardstatus) {
    if (card.code == formData.turnoverType) {
      return card.name;
    }
  }
  return formData.turnoverType;
}

let processedStatus = computed(() => {
  return chooseStatus();
});
function chooseStatus() {
  for (let card of status) {
    if (card.code == formData.status) {
      return card.name;
    }
  }
  return formData.status;
}
</script>
